package com.lou1052.core.constant.tlocal;

import com.esotericsoftware.kryo.Kryo;
import com.lou1052.core.base.thread.LazyPoolObjectThreadLocal;
import com.lou1052.kryo.KryoExt;
import com.lou1052.kryo.KryoExtFactory;
import com.lou1052.kryo.KryoExtPool;
import com.lou1052.kryo.KryoExtPoolQueueImpl;
import org.objenesis.strategy.StdInstantiatorStrategy;

import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * Created with IntelliJ IDEA.
 * User: liuqing
 * Date: 2015/11/3
 * Time: 22:47
 * To change this template use File | Settings | File Templates.
 */
public final class ThreadLocalKryo {
    /**
     * 创建kryo工厂类
     */
    private static final KryoExtFactory KRYO_FACTORY = () -> {
        KryoExt kryo = new KryoExt();
        //config kryo
        kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
        return kryo;
    };

    /**
     * 对象池
     */
    private static final KryoExtPool KRYO_POOL = new KryoExtPoolQueueImpl(KRYO_FACTORY,new ConcurrentLinkedQueue<>());

    /**
     *
     * 在web应用中的Filter中 KRYO_THREAD_LOCAL.remove();
     */
    public static final ThreadLocal<KryoExt> LOCAL_POOL_KRYO = new LazyPoolObjectThreadLocal<>(KRYO_POOL);

}
